#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    srand(time(0));
    ui->setupUi(this);
    setDirectButton(ui->upButton, ":/image/up.png", "w");
    setDirectButton(ui->downButton, ":/image/down.png", "s");
    setDirectButton(ui->leftButton, ":/image/left.png", "a");
    setDirectButton(ui->rightButton, ":/image/right.png", "d");
}
Widget::~Widget()
{
    delete ui;
}

void Widget::setDirectButton(QPushButton *button, const QString &filename, const QString &shortcut)
{
    //设置提示信息
    ui->increaseOpacity->setToolTip("This button will increase the opacity of the window");
    ui->increaseOpacity->setToolTipDuration(5000);

    button->setIcon(QIcon(filename));
    button->setIconSize(button->size());
    button->setShortcut(QKeySequence(shortcut));
    button->setAutoRepeat(true);
    button->setAutoRepeatDelay(0);      //只要按下按钮就产生“连发”
    button->setAutoRepeatInterval(100);
}


//改变按钮状态
void Widget::on_disableButton_clicked()
{
    if(ui->targetButton->isEnabled())
    {
        ui->targetButton->setDisabled(true);
        ui->disableButton->setText("enable A");
    }
    else
    {
        ui->targetButton->setDisabled(false);
        ui->disableButton->setText("disable A");
    }

}

void Widget::on_upButton_clicked()
{
    QPushButton* button = ui->targetButton;
    button->setGeometry(button->x(),button->y()-5,button->width(),button->height());
}
void Widget::on_downButton_clicked()
{
    QPushButton* button = ui->targetButton;
    button->setGeometry(button->x(),button->y()+5,button->width(),button->height());
}
void Widget::on_leftButton_clicked()
{
    QPushButton* button = ui->targetButton;
    button->setGeometry(button->x()-5,button->y(),button->width(),button->height());
}
void Widget::on_rightButton_clicked()
{
    QPushButton* button = ui->targetButton;
    button->setGeometry(button->x()+5,button->y(),button->width(),button->height());
}

//当鼠标按下按钮之后，按钮随机移动至一个地方，形成低配版“点不到”的效果
void Widget::on_randomMove_pressed()
{
    int newX = rand() % this->width();
    int newY = rand() % this->height();
    ui->randomMove->setGeometry(newX,newY,ui->randomMove->width(),ui->randomMove->height());
}

//增加不透明度
void Widget::on_increaseOpacity_clicked()
{
    qreal val = this->windowOpacity();
    if(val <= 0)
    {
        return;
    }
    this->setWindowOpacity(val+0.1);
}
//减小不透明度
void Widget::on_decreaseOpacity_clicked()
{
    qreal val = this->windowOpacity();
    if(val <= 0)
    {
        return;
    }
    this->setWindowOpacity(val-0.1);
}

//改变label的字体样式
void Widget::on_underlineButton_clicked()
{
    //要先获取字体设置
    QFont font(ui->label->font());
    if(ui->label->font().underline())
    {
        font.setUnderline(false);

    }
    else
    {
        font.setUnderline(true);
    }
    ui->label->setFont(font);
}

void Widget::on_boldButton_clicked()
{
    QFont font(ui->label->font());
    if(ui->label->font().bold())
    {
        font.setBold(false);
    }
    else
    {
        font.setBold(true);
    }
    ui->label->setFont(font);
}
